package com.demo.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.internal.logging.Log4JLoggerFactory;

import java.net.InetAddress;
import java.util.logging.Logger;

/**
 * Created by chenhe17596 on 2017/6/24.
 */
public class ServerHandler extends SimpleChannelInboundHandler<String> {
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        //接收收到消息直接打印输出
        System.out.println(ctx.channel().remoteAddress()+" say : " + msg);
        //返回客户端消息
        ctx.writeAndFlush("收到你的消息!\n");
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Remote Address : "+ctx.channel().remoteAddress()+" active!");
        ctx.writeAndFlush("欢迎 " + InetAddress.getLocalHost().getHostName()+" service! \n");

        super.channelActive(ctx);
    }
}
